home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / TinyMeter / Source / TinyMeter_main / initclean.c < prev    next >
C/C++ Source or Header  |  1997-02-27  |  12KB  |  447 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <dos/dos.h>
  4. #include <dos/dosextens.h>
  5. #include <intuition/intuition.h>
  6. #include <intuition/gadgetclass.h>
  7. #include <intuition/intuitionbase.h>
  8. #include <intuition/classusr.h>
  9. #include <intuition/imageclass.h>
  10. #include <intuition/gadgetclass.h>
  11. #include <intuition/cghooks.h>
  12. #include <intuition/icclass.h>
  13. #include <intuition/classes.h>
  14. #include <intuition/sghooks.h>
  15. #include <intuition/screens.h>
  16. #include <datatypes/datatypesclass.h>
  17. #include <datatypes/datatypes.h>
  18. #include <datatypes/pictureclass.h>
  19. #include <graphics/gfxbase.h>
  20. #include <graphics/text.h>
  21. #include <graphics/gfxmacros.h>
  22. #include <utility/tagitem.h>
  23. #include <utility/hooks.h>
  24. #include <string.h>
  25. #include <clib/macros.h>
  26. #include "gaugeclass.h"
  27. #include "launchclass.h"
  28. #include "tinymeter.h"
  29.  
  30. extern struct IntuitionBase *IntuitionBase;
  31.  
  32. struct Gadget my_drag=
  33. {
  34.     NULL,
  35.     0,1,
  36.     0,0,
  37.     GFLG_GADGHNONE,
  38.     GACT_IMMEDIATE,
  39.     GTYP_SYSGADGET|GTYP_WDRAGGING,
  40.     0,
  41.     0,
  42.     0,
  43.  
  44.     0,
  45.     0,
  46.     0xFFF2,
  47.     0
  48. };
  49.  
  50. struct Gadget my_close=
  51. {
  52.     NULL,
  53.     0,0,
  54.     16,16,
  55.     NULL,
  56.     GACT_IMMEDIATE,
  57.     GTYP_SYSGADGET|GTYP_CLOSE,
  58.     0,
  59.     0,
  60.     0,
  61.  
  62.     0,
  63.     0,
  64.     0xFFF1,
  65.     0
  66. };
  67.  
  68. struct Gadget my_size=
  69. {
  70.     NULL,
  71.     0,0,
  72.     16,16,
  73.     GFLG_GADGHNONE,
  74.     GACT_IMMEDIATE,
  75.     GTYP_SYSGADGET|GTYP_SIZING,
  76.     0,
  77.     0,
  78.     0,
  79.  
  80.     0,
  81.     0,
  82.     0xFFF3,
  83.     0
  84. };
  85.  
  86. void new_window_size(struct tm_sys_set *set,struct tm_data *data)
  87. {
  88.     struct Window   *win=data->win;
  89.     my_drag.Width   =win->Width;
  90.     my_drag.Height  =win->Height-17;
  91.     my_size.TopEdge =win->Height-16;
  92.     my_size.LeftEdge=win->Width -16;
  93. }
  94.  
  95. struct TextFont *loadFont(char *name, UWORD size)
  96. {
  97.     struct  TextFont     *tf;
  98.     struct  TextAttr     *my_text_attr;
  99.  
  100.     if(my_text_attr=(struct TextAttr *)pAllocVec(sizeof(struct TextAttr)))
  101.     {
  102.     my_text_attr->ta_Name =(UBYTE *)name;
  103.     my_text_attr->ta_YSize=(UWORD)size;
  104.     my_text_attr->ta_Flags=FPF_DISKFONT | FPB_PROPORTIONAL;
  105.  
  106.     tf=(struct TextFont *)OpenDiskFont((struct TextAttr *)my_text_attr);
  107.     pFreeVec((ULONG *)my_text_attr);
  108.     }
  109.     if(!tf) if(!(tf=(struct TextFont *)OpenTopaz())) return(0L);
  110.     return(tf);
  111. }
  112.  
  113. struct Window *open_new_window(struct tm_sys_set *set,struct tm_data *data, UWORD ysiz)
  114. {
  115.     struct Window *window;
  116.  
  117.     if((window=(struct Window *)OpenWindowTags(0,
  118.     WA_Left,            set->x_pos,
  119.     WA_Top,             set->y_pos,
  120.     WA_Width,           set->x_siz,
  121.     WA_MaxWidth,        4096,
  122.     WA_MaxHeight,       ysiz,
  123.     WA_MinHeight,       ysiz,
  124.     WA_Height,          ysiz,
  125.     WA_IDCMP,           NULL,
  126.     WA_MinWidth,        data->min_x_size,
  127.     WA_Flags,           (set->win_backfront!=win_back ? 0 : WFLG_BACKDROP )|WFLG_BORDERLESS|WFLG_RMBTRAP|WFLG_REPORTMOUSE,
  128.     WA_PubScreen,       data->scr,
  129.     TAG_DONE)))
  130.     {
  131.     if(data->appport=(struct MsgPort *)CreateMsgPort()) data->appwin=(struct AppWindow *)AddAppWindow(0L, 0L, window, data->appport , 0L);
  132.     my_drag.Width=   window->Width;
  133.     my_drag.Height=  window->Height-17;
  134.     my_size.TopEdge= window->Height-16;
  135.     my_size.LeftEdge=window->Width -16;
  136.     if(set->win_move==win_normal)
  137.     {
  138.         my_drag.NextGadget=NULL;
  139.         my_size.NextGadget=NULL;
  140.         my_close.NextGadget=NULL;
  141.         AddGadget(window,&my_drag, (UWORD)0);
  142.         AddGadget(window,&my_size, (UWORD)0);
  143.         AddGadget(window,&my_close,(UWORD)0);
  144.     }
  145.     ModifyIDCMP(window,IDCMP_NEWSIZE|IDCMP_CLOSEWINDOW|IDCMP_CHANGEWINDOW|IDCMP_SIZEVERIFY|IDCMP_VANILLAKEY|IDCMP_MOUSEBUTTONS|IDCMP_GADGETDOWN|IDCMP_GADGETUP);
  146.     return(window);
  147.     }
  148.     return(0L);
  149. }
  150.  
  151. struct Window *snapBackground(struct tm_sys_set *set,struct tm_data *data)
  152. {
  153.     struct Window *win=data->win;
  154.     UWORD x,y,xs,ys;
  155.     x=win->LeftEdge;
  156.     y=win->TopEdge;
  157.     xs=win->Width;
  158.     ys=win->Height;
  159.     set->x_pos=x;
  160.     set->y_pos=y;
  161.     set->x_siz=xs;
  162.     if(data->bg_bm) FreeBitMap(data->bg_bm); data->bg_bm=0;
  163.     if(data->bg_bm=(struct BitMap *)AllocBitMap(xs,ys,data->scr->RastPort.BitMap->Depth,0,0))
  164.     {
  165.     win->FirstGadget=NULL;
  166.     if(data->appwin)  RemoveAppWindow(data->appwin);
  167.     if(data->appport) DeleteMsgPort  (data->appport);
  168.     CloseWindow(win);
  169.     BltBitMap(data->scr->RastPort.BitMap,x,y,data->bg_bm,0,0,xs,ys,0xc0,0xff,0);
  170.     return((struct Window *)open_new_window(set,data,ys));
  171.     }
  172.     return(0L);
  173. }
  174.  
  175. void CopyTiledBitMap(struct BitMap *Src,WORD SrcSizeX,WORD SrcSizeY,WORD DstSizeX,WORD DstSizeY ,struct BitMap *Dst)
  176. {
  177.     WORD PosX,
  178.      PosY;
  179.     WORD SizeX,
  180.      SizeY;
  181.     for (PosX = 0,SizeX = MIN(SrcSizeX,DstSizeX);PosX<DstSizeX;)
  182.     {
  183.     for (PosY = 0,SizeY = MIN(SrcSizeY,DstSizeY);PosY<DstSizeY;)
  184.     {
  185.         BltBitMap(Src,0,0,Dst,PosX,PosY,SizeX,SizeY,0xC0,0xff,0L);
  186.         PosY += MIN(SizeY,DstSizeY-PosY);
  187.         SizeY = MIN(SizeY,DstSizeY-PosY);
  188.     }
  189.     PosX += MIN(SizeX,DstSizeX-PosX);
  190.     SizeX = MIN(SizeX,DstSizeX-PosX);
  191.     }
  192. }
  193.  
  194. Object *LoadImage(char *file, struct Screen *scr)
  195. {
  196.     Object              *dt_obj;
  197.  
  198.     if (dt_obj = (Object *)NewDTObject(file,
  199.     DTA_SourceType       ,DTST_FILE,
  200.     DTA_GroupID          ,GID_PICTURE,
  201.     PDTA_Remap           ,TRUE,
  202.     PDTA_Screen          ,scr,
  203.     PDTA_FreeSourceBitMap,TRUE,
  204.     OBP_Precision        ,PRECISION_IMAGE,
  205.     TAG_DONE))
  206.     {
  207.     if ( DoDTMethod(dt_obj,NULL,NULL,DTM_PROCLAYOUT,NULL,1))
  208.     {
  209.         return(dt_obj);
  210.     }
  211.     }
  212.     return(NULL);
  213. }
  214.  
  215. void fileBackground(struct tm_sys_set *set,struct tm_data *data)
  216. {
  217.     struct Window       *win=data->win;
  218.     struct BitMap       *work;
  219.     struct BitMapHeader *header;
  220.     UWORD xs,ys;
  221.  
  222.     if (data->bg_bm){ FreeBitMap(data->bg_bm); data->bg_bm=0L; }
  223.     if (data->dt_object = LoadImage(&set->bg_picture[0],data->scr))
  224.     {
  225.     GetDTAttrs(data->dt_object,PDTA_BitMapHeader,&header,PDTA_DestBitMap,&work,TAG_DONE);
  226.     if (work==FALSE) GetDTAttrs(data->dt_object,PDTA_BitMap,&work,TAG_DONE);
  227.     xs=win->Width;
  228.     ys=win->Height;
  229.     if(data->bg_bm=(struct BitMap *)AllocBitMap(xs,ys,work->Depth,0,0))
  230.     {
  231.         CopyTiledBitMap(work,header->bmh_Width,header->bmh_Height,xs,ys,data->bg_bm);
  232.     }
  233.     }
  234. }
  235.  
  236. UWORD calcXPos(UWORD i, struct tm_data *data, struct tm_sys_set *set)
  237. {
  238.     return((UWORD)(set->win_border_x+((i)%set->colums)*data->gauge_x_size+((i)%set->colums)*set->win_space_x));
  239. }
  240.  
  241. UWORD calcYPos(UWORD i, struct tm_data *data, struct tm_sys_set *set)
  242. {
  243.     UWORD tmp,j;
  244.     UWORD y_pos;
  245.  
  246.     tmp=i/set->colums;
  247.     if(set->lay_falling)
  248.     {
  249.     y_pos=set->win_border_y+(tmp*set->win_space_y);
  250.     for(j=(i-(tmp*set->colums));j<i;j+=set->colums) y_pos+=data->gauge_y_size_falling[j];
  251.     }
  252.     else
  253.     {
  254.     y_pos= set->win_border_y+(tmp*set->win_space_y)+((data->gauge_y_size[tmp]-data->gauge_y_size_falling[i])>>1);
  255.     for(j=0;j<tmp;j++) y_pos+=data->gauge_y_size[j];
  256.     }
  257.     data->gauge_y_pos[i]=y_pos;
  258.     return(y_pos);
  259. }
  260.  
  261. struct Window *openWindow(struct tm_sys_set *set,struct tm_data *data, Class *gclass, Class *lclass)
  262. {
  263.     UWORD               y_siz,
  264.             line,
  265.             i,opt1,opt2,
  266.             tmp;
  267.     struct tm_gau_set   *many;
  268.     struct RastPort     *tmpras;
  269.     BOOL                change;
  270.     if( data->scr=(struct Screen *)LockPubScreen((char *)&set->pub_name[0]))
  271.     {
  272.     data->on_public=TRUE;
  273.     }
  274.     else
  275.     {
  276.     data->scr=(struct Screen *)IntuitionBase->FirstScreen;
  277.     data->on_public=FALSE;
  278.     }
  279.     data->labelpos=0;
  280.     if(tmpras=(struct RastPort *)pAllocVec(sizeof(struct RastPort)))
  281.     {
  282.     InitRastPort(tmpras);
  283.     for(i=0;i<data->num_of_gaug;i++)
  284.     {
  285.         data->gauge_y_size[i]=0;
  286.         data->gauge_y_size_falling[i]=0;
  287.     }
  288.     for(i=0,many=data->list;i<data->num_of_gaug;i++)
  289.     {
  290.         if(many->type!=typ_none)
  291.         {
  292.         line=(i/(set->colums));
  293.         if(data->Font[i]=loadFont(many->font,many->font_size))
  294.         {
  295.             data->gauge_y_size_falling[i]=(data->Font[i]->tf_YSize*(many->size_y+100))/100;
  296.             data->gauge_y_size[line]=MAX(data->gauge_y_size[line],data->gauge_y_size_falling[i]);
  297.         }
  298.         if((many->gauge_type!=typ_histmeter)&&(many->type!=typ_clock_)&&(many->type!=typ_simplelauncher))
  299.         {
  300.             SetFont(tmpras,data->Font[i]);
  301.             tmp=TextLength(tmpras,many->label,my_strlen(many->label));
  302.             if(tmp>data->labelpos)data->labelpos=tmp;
  303.         }
  304.         }
  305.         else data->Font[i]=0;
  306.         many=many->next;
  307.     }
  308.     data->labelpos+=4;
  309.     pFreeVec((ULONG *)tmpras);
  310.     opt1=(set->win_space_x*(set->colums-1));
  311.     opt2=(set->win_border_x<<1);
  312.     data->num_of_rows   =((data->num_of_gaug-1)/set->colums)+1;
  313.     data->min_x_size    =((data->labelpos+8)*set->colums)+opt1+opt2;
  314.     if(data->min_x_size>set->x_siz)set->x_siz=data->min_x_size;
  315.     data->gauge_x_size  =((set->x_siz-opt2)-opt1)/set->colums;
  316.     allocGadgets(set,data,gclass,lclass);
  317.     change=FALSE;
  318.     for(i=0,many=data->list;i<data->num_of_gaug;i++)
  319.     {
  320.         if(data->gdg[i])
  321.         {
  322.         ULONG   height;
  323.         line=(i/(set->colums));
  324.         switch (many->type)
  325.         {
  326.             case    typ_simplelauncher:
  327.                 GetAttr(LAU_ImgHeight,data->gdg[i],&height);
  328.                 data->gauge_y_size[line]        = MAX(data->gauge_y_size[line],     height + 6);
  329.                 data->gauge_y_size_falling[i]   = MAX(data->gauge_y_size_falling[i],height + 6);
  330.                 SetAttrs(data->gdg[i],GA_Height,data->gauge_y_size_falling[i],TAG_DONE);
  331.                 change=TRUE;
  332.                 break;
  333.             case    typ_iconlauncher:
  334.                 {
  335.                 struct  lau_entry       *foobar;
  336.                 struct  BitMapHeader    *bmhd;
  337.                 ULONG                   height=0;
  338.                 GetAttr(LAU_List,data->gdg[i],(ULONG *)&foobar);
  339.                 if(foobar)
  340.                 {
  341.                     do
  342.                     {
  343.                     if(foobar->img)
  344.                     {
  345.                         GetDTAttrs(foobar->img,PDTA_BitMapHeader,&bmhd,TAG_DONE);
  346.                         if(bmhd)
  347.                         {
  348.                         height=MAX(height,bmhd->bmh_Height);
  349.                         }
  350.                     }
  351.                     }
  352.                     while(foobar=foobar->next);
  353.                     data->gauge_y_size[line]        = MAX(data->gauge_y_size[line],     height);
  354.                     data->gauge_y_size_falling[i]   = MAX(data->gauge_y_size_falling[i],height);
  355.                     SetAttrs(data->gdg[i],GA_Height,height,TAG_DONE);
  356.                     change=TRUE;
  357.                 }
  358.                 }
  359.                 break;
  360.         }
  361.         }
  362.         many=many->next;
  363.     }
  364.     if(change)
  365.         for(i=0;i<data->num_of_gaug;i++)
  366.         if(data->gdg[i]) SetAttrs(data->gdg[i],GA_Top,calcYPos(i,data,set),TAG_DONE);
  367.  
  368.     if(set->lay_falling)
  369.     {
  370.         UWORD old_siz,j;
  371.         for(i=0,y_siz=0;i<set->colums;i++)
  372.         {
  373.         old_siz=y_siz;
  374.         for(j=i,y_siz=0;j<data->num_of_gaug;j+=set->colums) y_siz+=data->gauge_y_size_falling[j];
  375.         if(y_siz>old_siz) old_siz=y_siz;
  376.         }
  377.         y_siz=old_siz;
  378.     }
  379.     else for(i=0,y_siz=0;i<data->num_of_rows;i++)y_siz+=data->gauge_y_size[i];
  380.     y_siz+=(set->win_border_y<<1)+(set->win_space_y*(data->num_of_rows-1));
  381.     if(data->win=(struct Window *)open_new_window(set,data,y_siz))
  382.     {
  383.         ULONG   list;
  384.         data->bg_color    =obtainPen(data->scr,&set->bg_color);
  385.         data->bright_color=obtainPen(data->scr,&set->bright_color);
  386.         data->dark_color  =obtainPen(data->scr,&set->dark_color);
  387.         switch (set->bg_type)
  388.         {
  389.         case    bg_file:
  390.             fileBackground(set,data);
  391.             break;
  392.         case    bg_snap:
  393.             data->win=snapBackground(set,data);
  394.             break;
  395.         }
  396.         drawBackground(set,data);
  397.         for(i=0;i<data->num_of_gaug;i++) if(data->gdg[i])
  398.         {
  399.         GetAttr(LAU_List,data->gdg[i],&list);
  400.         if(list) AddGList(data->win,data->gdg[i],0,1,NULL);
  401.         else     AddGList(data->win,data->gdg[i],-1,1,NULL);
  402.         }
  403.         RefreshGList(data->gdg[i],data->win,NULL,-1);
  404.         return(data->win);
  405.     }
  406.     }
  407.     return(0L);
  408. }
  409.  
  410. void closeWindow(struct tm_sys_set *set,struct tm_data *data)
  411. {
  412.     int i;
  413.     if(!set->bg_color.pen)     ReleasePen     (data->scr->ViewPort.ColorMap,data->bg_color);
  414.     if(!set->bright_color.pen) ReleasePen     (data->scr->ViewPort.ColorMap,data->bright_color);
  415.     if(!set->dark_color.pen)   ReleasePen     (data->scr->ViewPort.ColorMap,data->dark_color);
  416.     if(data->appwin)           RemoveAppWindow(data->appwin);
  417.     if(data->appport)          DeleteMsgPort  (data->appport);
  418.     if(data->win)
  419.     {
  420.     data->win->FirstGadget=NULL;
  421.     CloseWindow(data->win);
  422.     data->win=0L;
  423.     }
  424.     removeGadgets(set,data);
  425.  
  426.     for(i=0;i<data->num_of_gaug;i++) if(data->Font[i])
  427.     {
  428.     CloseFont(data->Font[i]);
  429.     data->Font[i]=0L;
  430.     }
  431.     if(data->on_public)
  432.     {
  433.     UnlockPubScreen((char *)&set->pub_name[0],NULL);
  434.     data->on_public=FALSE;
  435.     }
  436.     if(data->dt_object)
  437.     {
  438.     DisposeDTObject((Object *)data->dt_object);
  439.     data->dt_object=0L;
  440.     }
  441.     if(data->bg_bm)
  442.     {
  443.     FreeBitMap(data->bg_bm);
  444.     data->bg_bm=0L;
  445.     }
  446. }
  447.